using System;

using System.Xml;
using System.Diagnostics;
using System.IO;
using CUtil;
using CGraph;


namespace CStock
{

	/// <summary>
	/// Summary description for Configuration.
	/// </summary>
	public class Configuration : StockObjectBase , IConfiguration
	{
		#region Constants

		private const string DefaultConfigurationFileName = "StockGraphOptions.Xml";
		private const string DefaultXmlNodeNameRoot = "StockGraphConfiguration";
		private const string DefaultXmlNodeNameDateStart = "GraphDateStart";
		private const string DefaultXmlNodeNameDateEnd = "GraphDateEnd";

		#endregion

		#region Fields
        private Directories directories;
        private Dates dates;


        private bool showTradeEventsOnChart;
        private bool webUpdate_Only200;
        int chartInterval;
        private bool terminateAction;
        private ProgramModes programMode;//{get;set;}
        private bool showIndicatorsToShowOnChart;
        private bool tradeShortSellAllowed;
        private bool tradeStopLoss;
        private bool tradeStopWin;
        private int stopLossPercentage;
        private int stopWinPercentage;
        private bool showTradeSystemIndicator;
        private DateTime lastQuoteUpdate;
        private string webUpdateFileName;
        private bool tradeShortSellOtherFile;
        private int intradayDataSaveInterval;
        private bool intradayModus;
        private bool futuresTrade;

        private int tradeStopWinLossDynamicPercentage;
        private bool tradeStopWinLossDynamic;

        private System.Collections.ArrayList indicatorsOnChart;
		private System.Collections.ArrayList indicatorsIntradayOnChart;

		private TradeMoneyType tradeMoneyType;
			
		

		
	

		#endregion

		#region Constructor

	
		public Configuration()
		{
            ResetPathFromConfiguration();
            SetNewPath(this.path);

            this.iD = "Configuration";

            webUpdate_Only200 = false;
			this.indicatorsOnChart = new System.Collections.ArrayList();
			indicatorsIntradayOnChart = new System.Collections.ArrayList();
			this.programMode = ProgramModes.Realtime;
			this.tradeMoneyType = CUtil.TradeMoneyType.TradeWith10000;

            DateTime dt = DateTime.Now;
         
			
		}

   
	
		#endregion

        #region Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newPath"></param>
        /// <returns></returns>
        public bool SetNewPath(string newPath)
        {

            this.path = newPath;

            dates = new CStock.Dates();
            directories = new Directories(this.path);

            dates.Defaults();
            return DefaultRest();

        }


        private bool DefaultRest()
        {
            try
            {
                this.stopLossPercentage = 0;
                this.stopWinPercentage = 0;
                this.intradayDataSaveInterval = 300;  //in seconds - once per 5 minutes
                WebUpdateFileName = "Quotes.zip";
                intradayModus = false;



            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Configuration - Error initilizing Configuration: ", exc.Message);
                return false;
            }
            return true;
        }
        public override bool ResetPathFromConfiguration()
        {

            ProgramContext pc = ProgramContext.Instance;

            Debug.Assert(pc != null, "Initialize ProgramContext first ...");
            if (pc != null)
                this.path = ProgramContext.Instance.Path;
            return true;


        }
        #endregion

        #region Properties


        public Dates Dates
		{
			get
			{
				return this.dates;
			}
			set
			{
				this.dates = value;
			}
		}
     
		public Directories Directories
		{
			get
			{
				return this.directories;
			}
			set
			{
				this.directories = value;
			}
		}
		public int IntradayDataSaveInterval
		{
			get
			{
				return this.intradayDataSaveInterval;
			}
			set
			{
				this.intradayDataSaveInterval = value;
			}
		}
		
		public CUtil.TradeMoneyType TradeMoneyType
		{
			get
			{
				return this.tradeMoneyType;
			}
			set
			{
				this.tradeMoneyType = value;
			}
		}

		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public bool TerminateAction
		{
			get
			{
				return this.terminateAction;
			}
			set
			{
				this.terminateAction = value;
			}
		}
		//[System.Xml.Serialization.XmlIgnoreAttribute()]
		public bool ShowTradeEventsOnChart
		{
			get
			{
				return this.showTradeEventsOnChart;
			}
			set
			{
				this.showTradeEventsOnChart = value;
			}
		}
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public bool ShowTradeSystemIndicator
		{
			get
			{
				return this.showTradeSystemIndicator;
			}
			set
			{
				this.showTradeSystemIndicator = value;
			}
		}
		
		
	
	

	
		/// <summary>
		/// DateEnd
		/// </summary>
		public bool IntradayIndicators
		{
			get
			{
				return this.showIndicatorsToShowOnChart;
				
			}
			set
			{
				this.showIndicatorsToShowOnChart = value;
				
			}
		}
		

		public int ChartInterval
		{
			get
			{
				return this.chartInterval;
			}
			set
			{
				this.RaiseObjectChangedEvent(this,"ChartInterval",this.chartInterval,value);
				this.chartInterval = value;
				
			}
		}
		
	
		//[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Serialization.XmlForm.Unqualified, IsNullable=false)]
		public string[] IndicatorsToShowOnChartSerialized
		{
			get
			{
				return (string[])indicatorsOnChart.ToArray(typeof(string));
			}
			set
			{
				indicatorsOnChart.Clear();
				for(int i = 0; i < value.Length; i++)
				{
					indicatorsOnChart.Add(value[i]);
				}
			}
		}
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public System.Collections.ArrayList IndicatorsIntradayOnChart
		{
			get
			{
				return indicatorsIntradayOnChart;
			}
			set
			{
				indicatorsIntradayOnChart = value;
			}
		}
		

		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public System.Collections.ArrayList IndicatorsToShowOnChart
		{
			get
			{
				return indicatorsOnChart;
			}
			set
			{
				indicatorsOnChart = value;
			}
		}

		

		public bool  IntradayModus
		{
			get
			{
				return intradayModus;
			}
			set
			{
				intradayModus = value;
				this.RaiseObjectChangedEvent(this,"Intraday",this.intradayModus,value);

			}
		}
		public bool  IntradayModusSilent
		{
			get
			{
				return intradayModus;
			}
			set
			{
				intradayModus = value;
				
			}
		}
		

		public bool  FuturesTrade
		{
			get
			{
				return futuresTrade;
			}
			set
			{
				futuresTrade = value;
			}
		}
        public bool WebUpdate_Only200
        {
            get
            {
                return webUpdate_Only200;
            }
            set
            {
                webUpdate_Only200 = value;
            }
        }
		
	
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public ProgramModes ProgramMode 
		{
			get
			{
				return this.programMode;
			}
			set
			{
				this.programMode = value;
			}
		}
	
		/// <summary>
		/// 
		/// </summary>
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public bool ChartChanged
		{
			get
			{
				return false;
			}
			set
			{
				this.RaiseObjectChangedEvent(this,"ChartChanged",null,true);
			}
		}
		//	[System.Xml.Serialization.XmlIgnoreAttribute()]
		public bool TradeShortSellAllowed
		{
			get
			{
				return tradeShortSellAllowed;
			}
			set
			{
				this.tradeShortSellAllowed = value;
			}
		}
		public bool TradeShortSellOtherFile
		{
			get
			{
				return tradeShortSellOtherFile;
			}
			set
			{
				this.tradeShortSellOtherFile = value;
			}
		}
		
		public bool TradeStopLoss
		{
			get
			{
				return this.tradeStopLoss;
			}
			set
			{
				this.tradeStopLoss = value;
			}
		}
		public bool TradeStopWin
		{
			get
			{
				return this.tradeStopWin;
			}
			set
			{
				this.tradeStopWin = value;
			}
		}
		public bool TradeStopLossDynamic
		{
			get
			{
				return this.tradeStopWinLossDynamic;
			}
			set
			{
				this.tradeStopWinLossDynamic = value;
			}
		}
		public int TradeStopLossDynamicPercentage
		{
			get
			{
				return this.tradeStopWinLossDynamicPercentage;
			}
			set
			{
				this.tradeStopWinLossDynamicPercentage = value;
			}
		}
		
		public int TradeStopLossPercentage
		{
			get
			{
				return this.stopLossPercentage;
			}
			set
			{
				this.stopLossPercentage = value;
			}
		}
		public int TradeStopWinPercentage
		{
			get
			{
				return this.stopWinPercentage;
			}
			set
			{
				this.stopWinPercentage = value;
			}
		}
	
		public DateTime LastQuoteUpdate
		{
			get
			{
				return this.lastQuoteUpdate;
			}
			set
			{
				this.lastQuoteUpdate = value;
			}
		}

		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public string WebUpdateFileName
		{
			get
			{
				return this.webUpdateFileName;
			}
			set
			{
				this.webUpdateFileName = value;
			}
		}
	

		
	
		
		
		
		#endregion

      
	
	}
}
